﻿using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using ZL.Web.Utility.Cache;
using ZLWeb.WebApiEntityBase;

namespace ZLWeb.WebApiInfrastructure.Filter
{
    /// <summary>
    ///     SSL过滤器
    ///     todo 模仿MVC的SSL过滤器完善
    /// </summary>
    public class UseSSLAtrribute : AuthorizationFilterAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.All(tr => tr.Key != "requestguid"))
            {
                ResponseResultInfo error = new ResponseResultInfo();
                error.Description = "缺少RequestGuid拒绝访问!";
                actionContext.Response =
                actionContext.Request.CreateResponse(HttpStatusCode.NonAuthoritativeInformation, error);
                return;
            }
            string RequestGuid = actionContext.Request.Headers.GetValues("requestguid").FirstOrDefault();
            if (SERedisHelper.KeyExists(RequestGuid, 2))
            {
                ResponseResultInfo error = new ResponseResultInfo();
                error.Description = "RequestGuid存在,拒绝访问!";
                actionContext.Response =
                actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, error);
                return;
            }
            TimeSpan sd=new TimeSpan(10,0,0);
            SERedisHelper.Set(RequestGuid, 1, sd,2);
        }
    }
}